package com.glory.hzlionpda.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.hzlionpda.R;
import com.glory.hzlionpda.base.Constant;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.util.ArrayList;
import java.util.List;

public class DialogActivity extends FreeUI_BaseActivity {
    private List<FreeUI_ClearEditText> editTextList = new ArrayList<>();
    private List<LinearLayout> linearLayoutList = new ArrayList<>();
    private List<ImageView> imageViewList = new ArrayList<>();
    private List<String> stringList = new ArrayList<>();
    private FreeUI_GeneralFragmentDialog searchDialog, exitDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutResId() {
        return 0;
    }

    @Override
    protected void initView() {
        final String extra = getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY);
        if (extra.contains("SCAN1")) {
            final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
            View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.dialog_start_scan, null);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            final FreeUI_ClearEditText etUserId = view.findViewById(R.id.etUserId);
            final FreeUI_ClearEditText etLotId1 = view.findViewById(R.id.etLotId1);
            initMyView(view);
            etUserId.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (start == 0 && before == 0 && count <= 1) {
                        etUserId.setText("");
                    }

                    if (start == 0 && before == 0 && count > 1) {
                        etLotId1.requestFocus();
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            tvConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (etUserId.getText().toString().length() == 0) {
                        showToast("请扫描员工编号", false);
                        return;
                    }
                    StringBuffer sb = new StringBuffer();
                    for (String str : stringList) {
                        if (str.length() > 0) {
                            sb.append(str + ",");
                        }
                    }
                    if (sb.length() == 0) {
                        showToast("请扫描批次编号", false);
                        return;
                    }
                    String lots = sb.substring(0, sb.length() - 1);
                    Intent intent = new Intent();
                    Constant.CURRENT_USER = etUserId.getText().toString();
                    intent.putExtra("lots", lots);
                    intent.putExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY, "header");
                    intent.setClass(DialogActivity.this, StartLotActivity.class);
                    startActivity(intent);
                    freeUI_generalFragmentDialog.dismiss();
                }
            });
            tvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    freeUI_generalFragmentDialog.dismiss();
                }
            });
            freeUI_generalFragmentDialog.setLayout(view);
            freeUI_generalFragmentDialog.setCallBack(new FreeUI_CommonCallback() {
                @Override
                public void onSuccess() {
                    DialogActivity.this.finish();
                }
            });
            freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "eqp_scan_dialog");
        } else if (extra.contains("SCAN2")) {
            final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
            View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.dialog_scan_thing, null);
            TextView tvCancel = view.findViewById(R.id.tvCancel);
            TextView tvConfirm = view.findViewById(R.id.tvConfirm);
            final FreeUI_ClearEditText etEqpId = view.findViewById(R.id.etEqpId);
            final FreeUI_ClearEditText etLotId = view.findViewById(R.id.etLotId);
            final FreeUI_ClearEditText etUserId = view.findViewById(R.id.etUserId);
            etEqpId.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (start == 0 && before == 0 && count > 1) {
                        etLotId.requestFocus();
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            etLotId.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (start == 0 && before == 0 && count > 1) {
                        etUserId.requestFocus();
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });

            etUserId.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                    if (start == 0 && before == 0 && count <= 1) {
                        etUserId.setText("");
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });
            tvConfirm.setOnClickListener(view1 -> {
                if (etEqpId.getText().toString().length() == 0) {
                    showToast("请扫描设备编号", false);
                    return;
                }
                if (etLotId.getText().toString().length() == 0) {
                    showToast("请扫描批次编号", false);
                    return;
                }
                if (etUserId.getText().toString().length() == 0) {
                    showToast("请扫描员工编号", false);
                    return;
                }
                Intent intent = new Intent();
                Constant.CURRENT_USER = etUserId.getText().toString();
                intent.putExtra("eqpId", etEqpId.getText().toString().trim());
                intent.putExtra("lotId", etLotId.getText().toString().trim());
                intent.putExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY, "header");
                intent.setClass(DialogActivity.this, EndLotActivity.class);
                startActivity(intent);
                freeUI_generalFragmentDialog.dismiss();
            });
            tvCancel.setOnClickListener(v -> freeUI_generalFragmentDialog.dismiss());
            freeUI_generalFragmentDialog.setLayout(view);
            freeUI_generalFragmentDialog.setCallBack(DialogActivity.this::finish);
            freeUI_generalFragmentDialog.show(getSupportFragmentManager(), "eqp_scan_dialog");
        } else if (extra.contains("注销")) {
            exitDialog = FreeApi_DialogUtils.getConfirmAndCancelDialog(this, "确定要注销当前账号吗？", () -> {
                startActivity(new Intent(DialogActivity.this, LoginActivity.class));
                FreeApi_ActivityManager.getActivityManager().AppExit(DialogActivity.this);
                exitDialog.dismiss();
            }, () -> exitDialog.dismiss());
            exitDialog.setCallBack(DialogActivity.this::finish);
            exitDialog.show(getSupportFragmentManager(), "exit_dialog");
        } else if (extra.contains("SEARCH")) {
            searchDialog = getDialog(new FreeUI_TextCallback() {
                @Override
                public void onSuccess(String s) {
                    searchDialog.dismiss();
                    Intent intent = new Intent();
                    if (extra.contains("批次信息查询")) {
                        intent.setClass(DialogActivity.this, SearchLotActivity.class);
                    } else if (extra.contains("设备信息查询")) {
                        intent.setClass(DialogActivity.this, SearchEqpActivity.class);
                    }
                    intent.putExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY, "header");
                    Constant.CURRENT_USER = s;
                    startActivity(intent);
                }
            });
            searchDialog.setCallBack(() -> DialogActivity.this.finish());
            searchDialog.show(getSupportFragmentManager(), "searchDialog");
        }
    }

    private FreeUI_GeneralFragmentDialog getDialog(FreeUI_TextCallback editTextScanCallback){
        final FreeUI_GeneralFragmentDialog freeUI_generalFragmentDialog = new FreeUI_GeneralFragmentDialog();
        View view = LayoutInflater.from(DialogActivity.this).inflate(R.layout.dialog_address, null);
        TextView tvConfirm = view.findViewById(R.id.tvConfirm);
        TextView tvCancel = view.findViewById(R.id.tvCancel);
        final FreeUI_ClearEditText etIp = view.findViewById(R.id.etIp);
        etIp.setHint("请扫描操作人员编号");
        etIp.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count <= 1) {
                    etIp.setText("");
                }

                if (start == 0 && before == 0 && count > 1) {
                    editTextScanCallback.onSuccess(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        tvConfirm.setOnClickListener(view12 -> {
            if (!etIp.getText().toString().isEmpty()){
                editTextScanCallback.onSuccess(etIp.getText().toString().trim());
            }
        });
        tvCancel.setOnClickListener(view1 -> freeUI_generalFragmentDialog.dismiss());
        freeUI_generalFragmentDialog.setLayout(view);
        return freeUI_generalFragmentDialog;
    }

    private void initMyView(View view) {
        for (int i = 0; i < 12; i++) {
            stringList.add("");
            String tag1 = "etLotId" + (i + 1);
            FreeUI_ClearEditText freeUI_clearEditText = view.findViewWithTag(tag1);
            final int finalI = i;
            freeUI_clearEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (start == 0 && before == 0 && count > 1) {
                        stringList.set(finalI, s.toString());
                        if (finalI + 1 < 12) {
                            linearLayoutList.get(finalI + 1).setVisibility(View.VISIBLE);
                            editTextList.get(finalI + 1).requestFocus();
                        }
                    }
                    if (s.toString().length() == 0) {
                        stringList.set(finalI, "");
                    }
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
            editTextList.add(freeUI_clearEditText);

            String tag2 = "llLotScan" + (i + 1);
            LinearLayout linearLayout = view.findViewWithTag(tag2);
            linearLayoutList.add(linearLayout);

            String tag3 = "ivDelete" + (i + 1);
            ImageView imageView = view.findViewWithTag(tag3);
            imageView.setOnClickListener(view1 -> {
                if (finalI + 1 < 12 && linearLayoutList.get(finalI + 1).getVisibility() == View.VISIBLE) {
                    showToast("请先删除上一个", false);
                } else {
                    editTextList.get(finalI).setText("");
                    linearLayoutList.get(finalI).setVisibility(View.GONE);
                    stringList.set(finalI, "");
                }
            });
            imageViewList.add(imageView);
        }

    }


    @Override
    protected void initData() {

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }
}
